$('#newNeeds').modal({
	    	show: false
	    });
		$('#add-guest-btn').on('click',function(){
			$("input[name='com_name']").val("");
			$("input[name='id']").val(0);
			$("#sort").val(0);
			$("#content").val("");
			$("#contact").val("");
		    $('#newNeeds').modal('show');
		});
		function getItem(){
			var $item = $(this).parents("tr");
			var itemId = $item.children("td:first").html();
			$.ajax({
			        url:"/web/category/getItem",
			        data:"itemId="+itemId,
			        type:"get", 
			        success:function(data){
			        	$("input[name='id']").val(data.data.id);
			        	$("textarea[name='content']").val(data.data.content);
			        	$("#sort").val(data.data.sort);
			        	var attrs = JSON.parse(data.data.extendAttrs);
			        	$("#com_name").val(attrs.com_name);
			        	$("#contact").val(attrs.contact);
			        	$(".form-control[name='categoryId']").val(data.data.categoryId);
						$('#newNeeds').modal('show');
			        }  
			 }); 
			return false;
		}
		function deleteItem(){
			var $item = $(this).parents("tr");
			var itemId = $item.children("td:first").html();
			actionConfirm({
				msg: "确定要删除吗？",
				confirm: function() {
					$.ajax({  
					        url:"/web/category/deleteItem", 
					        data:"itemId="+itemId,
					        type:"post",   
					        success:function(data){  
					           $item.remove();
					        }  
					 }); 
				}
			});
		}
		$(".btn-primary-save").click(function(){
			var id = $("#id").val();
			var categoryName = $(".form-control option:selected").html();
			var data = {},attrs={},met = "";
			data['id'] = id;
			data['content'] = $("#content").val();
			data['categoryId'] = $(".form-control option:selected").val();
			attrs['com_name'] = $("#com_name").val();
			attrs['contact'] = $("#contact").val();
			data['extendAttrs'] = JSON.stringify(attrs);
			
			if(id == 0){
				met = "addItem";
			}else{
				met = "editItem";
			}
			$.ajax({  
			        url:$(".form-horizontal").attr("action")+met,
			        data:data,
			        type:"post",
			        success:function(obj){
			        	if(id != 0){
			        		var $item = $("td:contains('"+id+"')").parents("tr"); 
			        		var str = "<td>" + data['id'] + "</td><td>"+categoryName+"</td><td>" + attrs['com_name'] + "</td><td>" + attrs['contact'] + "</td><td>"+data['content']+"</td><td><a href='/web/advert/edit?itemId="+ id +"' class='btn btn-xs btn-primary btn-primary-edit'>编辑</a><a href='javascript:;' class='btn btn-xs btn-danger news-delete-btn'>删除</a></td>";
			        		$item.empty();
			        		$item.append(str);
			        		
			        	}else{
				            var str ="<tr><td>" + obj.data + "</td><td>"+categoryName+"</td><td>" + attrs['com_name'] + "</td><td>" + attrs['contact'] + "</td><td>"+data['content']+"</td><td><a href='/web/advert/edit?itemId="+ id +"' class='btn btn-xs btn-primary btn-primary-edit'>编辑</a><a href='javascript:;' class='btn btn-xs btn-danger news-delete-btn'>删除</a></td></tr>";
				            $(".table-hover tbody").prepend(str);
				            $(".table-hover tbody tr:last-child").remove();
				            window.location.reload();
						}
						$(".news-delete-btn").click(deleteItem);
						$(".btn-primary-edit").click(getItem);
						$('#newNeeds').modal('hide');
			        }  
			 });  
			return false;
		});
		$(".news-delete-btn").click(deleteItem);
		$(".btn-primary-edit").click(getItem);